package com.liferay.controller;

import java.util.ArrayList;
import java.util.List;

import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import com.liferay.contant.Contants;
import com.liferay.model.Subject;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ParamUtil;
import com.liferay.portal.model.Role;
import com.liferay.portal.model.User;
import com.liferay.portal.service.RoleLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.service.RegisterSubjectService;
import com.liferay.service.impl.RegisterSubjectServiceImpl;

@Controller(value="MarkController")
@RequestMapping("VIEW")
public class MarkController{
	
	private static Logger log = Logger.getLogger(MarkController.class);
	
	private RegisterSubjectService registerSubjectService;
	private String email="";
	private List<Subject> list;
	private Subject object;
	
	@RenderMapping
	public String handleRenderRequest(RenderRequest request, RenderResponse response, Model model) throws NumberFormatException, SystemException, PortalException{
		log.info("show view default");
		String userId = request.getRemoteUser();
		if(userId!=null){
			List<Role> listRole = RoleLocalServiceUtil.getUserRoles(Long.parseLong(userId));
			String nameRole = listRole.get(0).getName();
			if (nameRole.equalsIgnoreCase("Administrator")) {
				return "search";
			}
			for(Role r : listRole){
				if (r.getName().equalsIgnoreCase(Contants.ROLE_STUDENT)) {
					User user = UserLocalServiceUtil.getUser(Long.parseLong(userId));
					email =  user.getEmailAddress();
					registerSubjectService = new RegisterSubjectServiceImpl();
					list = registerSubjectService.getListSubjectOpenRegisterByEmail(email);
					model.addAttribute("listSubject", list);
					model.addAttribute("role", "");
					log.info("show mark");
					return "view2";
				}
			}
		}
		return "view1";
	}

	@ActionMapping(params = "action=markforguest")
	public void getStudentID(ActionRequest request, ActionResponse response) {
		long id = ParamUtil.getLong(request, "studentid");
		email= id+"@hcmuaf.edu.vn";
		response.setRenderParameter("action", "showmarkd");
	}
	
	
	@ActionMapping(params = "action=getEmailStudentSearch")
	public void getEmailStudentSearch(ActionRequest request, ActionResponse response){
		long id = ParamUtil.getLong(request, "studentid");
		email= id+"@hcmuaf.edu.vn";
		response.setRenderParameter("action", "showmarkd");
	}
	
	
	@RenderMapping(params= "action=showmarkd")
	public String handleRenderShowMark(RenderRequest request, RenderResponse response, Model model) throws NumberFormatException, PortalException, SystemException{
		registerSubjectService = new RegisterSubjectServiceImpl();
		list = registerSubjectService.getListSubjectOpenRegisterByEmail(email);
		model.addAttribute("listSubject", list);
		model.addAttribute("role", Contants.ADMIN);
		log.info("show mark");
		return "view2";
	}
	
	@ActionMapping(params = "action=editmarkd")
	public void handleActionEditMark(ActionRequest request, ActionResponse response) {
		long idSubject = ParamUtil.getLong(request, "idSubject");
		long idSubjectOpen = ParamUtil.getLong(request, "idSubjectOpen");
		object = new Subject();
		object.setIdSubject(idSubject);
		object.setIdSubjectOpen(idSubjectOpen);
		
		
		log.info(" Student ID:" + idSubject); 
		response.setRenderParameter("action", "editmarkrender");
	}
	
	@RenderMapping(params= "action=editmarkrender")
	public String handleRenderEditMark(RenderRequest request, RenderResponse response, Model model){
		log.info("save edit");
		List<Subject> listSubject=new ArrayList<Subject>();
		for(Subject s : list){
			if(s.getIdSubject()==object.getIdSubject() && s.getIdSubjectOpen()==object.getIdSubjectOpen()){
				listSubject.add(s);
				break;
			}
		}
		model.addAttribute("listSubject", listSubject);
		return "edit";
	}
		
	@ActionMapping(params = "action=editsavemarkd")
	public void handleActionEditSaveMark(ActionRequest request, ActionResponse response) {
		double mark = ParamUtil.getDouble(request, "mark");
		long fkStudent = ParamUtil.getLong(request, "fkStudent");
		long idSubjectOpen = ParamUtil.getLong(request, "idSubjectOpen");
		Subject s = new Subject();
		s.setMark(mark);
		s.setFkStudent(fkStudent);
		s.setIdSubjectOpen(idSubjectOpen);
		registerSubjectService = new RegisterSubjectServiceImpl();
		registerSubjectService.updateSubjectMark(s);
		s=null;
		log.info(" mark by subject:" + mark); 
		response.setRenderParameter("action", "showmarkd");
	}
}
